package com.flower.core_service.controller;

import cn.hutool.core.util.ObjectUtil;
import com.common.baseModel.JsonResult;
import com.flower.core_service.feignClient.UploadFeignClient;
import com.flower.core_service.sources.CoreSource;
import com.flower.oss.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.Future;

/**
 *@ClassName TestController
 *@Description TODO
 *@Author 吃麻椒的花酱
 *@Date 2022/8/12 16:09
 *@Version 1.0
 */
@Api("测试")
@Slf4j
@RestController("/coreService/testcontroller")
public class TestController {
    @Qualifier("com.flower.core_service.feignClient.UploadFeignClient")
    @Autowired
    UploadFeignClient uploadFeignClient;

    @Autowired
    Source source;

    @Resource
    OssTemplate ossTemplate;

    @Autowired
    CoreSource coreSource;
    @ApiOperation("删除测试")
    @GetMapping("/testdelete")
    public JsonResult<Object> deleteTest(){
        uploadFeignClient.test();
        return JsonResult.isOk();
    }

    @ApiOperation("生产者")
    @GetMapping("/send")
    /**
     * @author 吃麻椒的花酱
     * @date 2022/8/12 16:46
     * @Description TODO
     * @param apiOperation
     * @param b
     * @return com.common.utils.ResponseResult
     */
    public JsonResult<Object> send(String apiOperation, String b){
         MessageBuilder<String> builder = MessageBuilder.withPayload("topictest");
         Message<String> message = builder.build();
         source.output().send(message);
         coreSource.output().send(message);
        System.out.println("消息发送成功");
        ObjectUtil.equals(',','，');
         return JsonResult.createSuccess("消息发送成功");
    }

    @GetMapping("/asyncTest")
    public JsonResult<Object> asyncTest(){
        log.info(Thread.currentThread().getName()+"asyncTest-----");
        Future<String> asyncResult=getFuture();
        String res="";
        try {
             res=asyncResult.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.isOk();

    }
// docker run  -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=121.40.117.198:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://121.40.117.198:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka


    @Async(value ="executeFlower" )
    public Future<String> getFuture(){
        log.info(Thread.currentThread().getName()+"future--------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return new AsyncResult<>("getFuture");
    }


}
